package com.example.cxx.controller;

import com.example.cxx.Utils.CookieUtil;
import com.example.cxx.pojo.Exam;
import com.example.cxx.pojo.ExamTerm;
import com.example.cxx.pojo.Result;
import com.example.cxx.pojo.YuMing;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("course/api/*")
public class ExamController {
    @GetMapping("examTerms")
    public Result examInfo(String cookie) throws IOException {
        if("".equals(cookie)){

        }
        try {
            Document index=Jsoup.connect(YuMing.qianzhui+YuMing.yuming+"/EaWeb/Manager/Module/NetEa/Examination/Search/Query/Menu.aspx").cookies(CookieUtil.convertCookie(cookie)).get();
            if(index.getElementById("Account")!=null&&"".equals(index.getElementById("Account").val())){
                return Result.error("LOGIN-FAIL");
            }
            Elements optionTerms=index.getElementById("DropDownListXnXq").children();
            List<ExamTerm> examTerms = new ArrayList<>();
            for (Element optionTerm : optionTerms) {
                if("空".equals(optionTerm.text())){
                    continue;
                }
                ExamTerm examTerm=new ExamTerm(optionTerm.text());
                examTerms.add(examTerm);
            }
            return Result.success(examTerms);
        }
        catch (Exception e){
            return Result.error("LOGIN-FAIL");
        }

    }
    @GetMapping("examByTerm")
    public Result getExamInfoByTerm(String cookie,String xn,String xq,String key) throws IOException {
        try {
            Connection connect = Jsoup.connect(YuMing.qianzhui+YuMing.yuming+"/EaWeb/Manager/Module/NetEa/Examination/Search/Query/List.aspx").cookies(CookieUtil.convertCookie(cookie));
            //添加参数
            connect.data("fyh","00").data("xn",xn).data("xq", xq).data("type","Student").data("key",key);
            Connection.Response response = connect.method(Connection.Method.GET).ignoreContentType(true).followRedirects(true).execute();
            //获取数据，处理成HTML
            Document document = response.parse();
            Element examTable=document.getElementById("GrwExam");//考试信息表
            //校验过期
            if(examTable==null){
                return Result.error("LOGIN-FAIL");
            }
            Elements nullExam=examTable.children();
            //校验是否空考试安排
            for (Element element : nullExam) {
                if(element.text().contains("没有考试安排数据")){
                    return Result.success(element.text());
                }
            }
            //抓取课程
            List<Exam> examsList=new ArrayList<>();//所有自己班的考试信息
            Elements examTableItem1=examTable.getElementsByClass("row1");
            Elements examTableItem2=examTable.getElementsByClass("row");
            addExam(key, examsList, examTableItem1);
            addExam(key, examsList, examTableItem2);
            return Result.success(examsList);
        }
        catch (Exception e){
            return Result.error("LOGIN-FAIL");
        }

    }

    private void addExam(String key, List<Exam> examsList, Elements examTableItem1) {
        for (Element element : examTableItem1) {
            if(key.contains(element.child(2).text())){
                Exam exam=new Exam();
                exam.setExamName(element.child(0).text());
                exam.setExamPlace(element.child(4).text()+" "+element.child(5).text());
                exam.setExamTime(element.child(10).text()+" "+element.child(11).text());
                exam.setExamType(element.child(13).text());
                examsList.add(exam);
            }
        }
    }
}
